package controller

import (
	"gitee.com/molonglove/goboot/gin"
	"helper-api/app/constant"
	"helper-api/app/models/request"
	"helper-api/app/models/response"
	"helper-api/app/service"
	"helper-api/utils"
	"net/http"
	"strconv"
)

var Resource = new(PermissionController)

type PermissionController struct{}

// CreatePermission 创建资源
func (p *PermissionController) CreatePermission(c *gin.Context) {
	var (
		customErr *response.BusinessError
		param     request.ResourceCreateRequest
		result    *response.PermissionListResponse
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Permission.CreatePermission(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// DeleteResource 删除资源
func (p *PermissionController) DeleteResource(c *gin.Context) {
	var (
		userId    int64
		param     string
		ids       []int64
		customErr *response.BusinessError
	)
	if param = c.DefaultQuery("ids", ""); param == "" {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if ids = utils.StrToArray(param); len(ids) <= 0 {
		c.JSON(http.StatusOK, response.Ok("暂无数据需要操作"))
		return
	}
	userId = c.GetInt64(constant.HeaderUserKey)
	if customErr = service.Permission.DeleteResource(ids, userId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(response.Menus[response.DeleteSuccess]))
}

// UpdatePermission 更新资源
func (p *PermissionController) UpdatePermission(c *gin.Context) {
	var (
		customErr *response.BusinessError
		result    *response.PermissionListResponse
		param     request.ResourceUpdateRequest
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Permission.UpdatePermission(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// List 获取资源
func (p *PermissionController) List(c *gin.Context) {
	var (
		list      []response.PermissionListResponse
		customErr *response.BusinessError
		pIdStr    string
		pId       int64
	)
	pIdStr = c.DefaultQuery("pId", "0")
	pId, _ = strconv.ParseInt(pIdStr, 10, 64)
	if list, customErr = service.Permission.List(pId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(list))
}

// LevelTree 资源层级查询
func (p *PermissionController) LevelTree(c *gin.Context) {
	var (
		list      []response.PermissionTreeProp
		customErr *response.BusinessError
		pIdStr    string
		allStr    string
		pId       int64
		all       bool
	)
	pIdStr = c.DefaultQuery("pId", "0")
	allStr = c.DefaultQuery("all", "false")
	pId, _ = strconv.ParseInt(pIdStr, 10, 64)
	all, _ = strconv.ParseBool(allStr)
	if list, customErr = service.Permission.Tree(pId, all); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(list))
}

// Info 详情
func (p *PermissionController) Info(c *gin.Context) {
	var (
		result    response.PermissionInfoResponse
		customErr *response.BusinessError
		pIdStr    string
		ok        bool
		pId       int64
	)
	if pIdStr, ok = c.GetQuery("pId"); !ok {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	pId, _ = strconv.ParseInt(pIdStr, 10, 64)
	if result, customErr = service.Permission.Info(pId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}
